package org.erepublik.company.service;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

import org.erepublik.company.ECountry;
import org.erepublik.company.EProductType;
import org.erepublik.company.Tax;
import org.erepublik.company.Taxes;
import org.erepublik.company.generated.CountryTaxesT;
import org.erepublik.company.generated.ProductTaxesT;
import org.erepublik.company.generated.TaxesT;
import org.erepublik.company.xml.XMLParsersers;

/**
 * Klasa dostarcza stawki podatkowe z ustalonego pliku xml
 *
 * @author Kacper Gorski 'Railman' railman85@gmail.com
 */
public class XmlVatService implements IVatService {

	private HashMap<ECountry, Taxes> map = null;
	public HashMap<ECountry, Taxes> getVat() throws Exception {
		if (map!=null)
			return map;
		
		HashMap<ECountry, Taxes> map = new HashMap<ECountry, Taxes>();
		InputStream inputStram = getClass().getResourceAsStream("/xmlSamples/vat.xml");
		TaxesT vatList = XMLParsersers.parseTaxes(inputStram);
		for (CountryTaxesT tax : vatList.getCountryTax()) {
			Taxes taxes = new Taxes();
			ECountry country = ECountry.valueOf(tax.getCountry());
			taxes.setCountry(country);
			List<ProductTaxesT> products = tax.getProductTax();
			for (ProductTaxesT productTaxesT : products) {
			    EProductType producttype = EProductType.valueOf( productTaxesT.getProduct().trim() );
                taxes.setTaxes(producttype, new Tax(productTaxesT.getVat(), productTaxesT.getImportTax(), productTaxesT.getIncomeTax()));
            }
			map.put(country,taxes);
		}
		
		return map;
	}

}
